package com.entities.projectversion.requestmapparser;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.model.project.Version;


public class ProjectVersionMapParser {

	public static Version fromMap ( Map <String, String[]> map ) {


		boolean errorFound = false;


		String name = null;
		try {
			name = map.get("name")[0];
			if ( name.trim().length() == 0 ) {
				name = null;
			}
		} catch ( ArrayIndexOutOfBoundsException e ) {
		}

		String strDate = null;
		Date date=null;
		try {
			strDate = map.get("date")[0];
			if ( strDate == null ) {
				date = null;
			}

			try {
				DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
				Calendar cal = dateFormat.getCalendar();
				cal.setLenient(false);
				dateFormat.setCalendar(cal);
				date = dateFormat.parse(strDate);

			} catch ( ParseException e ) {
				date = null;
			}

		} catch ( ArrayIndexOutOfBoundsException e ) {
		}
		
		String description = null;
		try {
			description = map.get("description")[0];
			if ( description.trim().length() == 0 ) {
				description = null;
			}
		} catch ( ArrayIndexOutOfBoundsException e ) {
		}

		if ( errorFound ) {
		}


		Version template = null;
		template = new Version();
		template.setName(name);
		template.setReleaseDate(date);
		template.setDescription(description);

		return template;

	}

}
